<?php
    session_start();  
    require '../crud/CustomSecurity.class.php';
    require '../common/Utility.php';
    require '../crud/Users.class.php';
    require '../crud/Userdata.class.php';
    require '../crud/Matches.class.php';
    require '../crud/Prognosis.class.php';
    require '../db/configuration.php';
    loginCheck();
    loadDB();
    isAdmin();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="bg">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <link rel="stylesheet" type="text/css" href="../css/page.css" />
    <link rel="stylesheet" type="text/css" href="../css/main.css" />
    <link rel="stylesheet" type="text/css" href="../css/generalProduct.css" />
    <link rel="stylesheet" type="text/css" href="../css/add-product.css" />

<!--    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>-->
<!--  	<script type="text/javascript" src="js/autocomplete/jquery.autocomplete.js"></script>-->
	<title>Резултат</title>
	<meta name="author" content="Dimtiar Giormov">
	
</head>
    <body>
    <?php computePoints();?>
<?php
function computePoints(){
  	$prognosis = new Prognosis(con());
  	$fMatches = new Matches(con());
  	$finished = $fMatches->getAllFinished();
  	
  	foreach ($finished as $fMatch1){
  		echo '<br/>Finished Match<br/>';
		echo "match id: $fMatch1[0] resA: $fMatch1[1] resB $fMatch1[2]<br/>";
		$id = $fMatch1[0]; 
		$resA = $fMatch1[1];
		$resB = $fMatch1[2];
		$pMatches0 = $prognosis->getMatches($id);
  		$pMatches = $pMatches0;
  		foreach ($pMatches as $pMatch){
  			echo '<br/>Prognosis:<br/>';
  			echo "prog id: $pMatch[2] resA: $pMatch[0] resB $pMatch[1]<br/>";
  			$points = 0;
  			if ($resA == -1){
  				$points = 0;
  			} else if ($pMatch[0] == $resA && $pMatch[1] == $resB){
  				$points = 5;
  			} else if( ($pMatch[0] == $pMatch[1] && $resA == $resB) ||
  			($pMatch[0] - $pMatch[1] == $resA - $resB)){
  				$points = 4;
  			} else if($pMatch[0] - $pMatch[1] > 0 && $resA - $resB > 0 ||
  			$pMatch[0] - $pMatch[1] < 0 && $resA - $resB < 0){
  				$points = 3;
  			}
  			//  		if ($points >0){
  			$prognosis->updateScores($pMatch[2], $points);
  			//  		}

  		}
  	}
  	transferPoints();
  }
  
  function transferPoints(){
  	$users = new Users(con());
  	$ids = $users->getIds();
  	$userdata = new Userdata(con());
  	$progn = new Prognosis(con());
  	foreach($ids as $id){
  		$id1 = $id[0];
  		$points = $progn->getPointsPerId($id1);
  		$pts = $points[0][0];
  		$userdata->updatePoints($id1, $pts);
  		echo "<br/> uid: $id1 - points: $pts<br/>";
  	}
  }
?>
</body>
</html>